package com.freecloud.javabasics.thread.result;

import com.freecloud.common.LoggerUtil;

import java.util.concurrent.*;

/**
 * @Author: maomao
 * @Date: 2021-03-24 07:59
 */
public class FutureTest {

    public static void main(String[] args) throws Exception {

        ExecutorService executorService = Executors.newCachedThreadPool();

        Future<Integer> future = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Thread.sleep(5000);
                if(true)
                    throw new RuntimeException("sssss");
                return 20;
            }
        });
        //指定超时
        //Integer result = future.get(1,TimeUnit.SECONDS);
        //不超时
        //Integer result = future.get(10,TimeUnit.SECONDS);
        //直接获取结果
        Integer result = future.get();

        LoggerUtil.printThread(result.toString());

        //关闭线程池
        executorService.shutdown();
    }
}
